Skip to content

Purge element tags when Craft invalidates element caches - #1

Closed
timkelty wants to merge 3 commits into
mainfrom
element-invalidation
Closed

timkelty wants to merge 3 commits into
mainfrom
element-invalidation

Conversation

@timkelty

@timkelty timkelty commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Purges a site's cached pages when Craft invalidates element caches.

  • Listener: on ElementCachesInvalidated, queues a tag purge on the default client with the same element tags core collects from queries, such as element::5 and element::CraftCms\Cms\Entry\Elements\Entry::*. Drafts and revisions are skipped, and so are clients that can't purge by tag.
  • Flushing: HttpCache::flush() sends every client's queued purges once, after the request or console command and after each queued job. A failed purge is reported rather than thrown, so a CDN outage doesn't fail the save.

Asset, transform, folder and URI purges come next. They'll listen to core's change events once that PR lands.

What this package is

FOSHttpCache is a framework-agnostic library for talking to caching proxies:

  • invalidation clients for Varnish, NGINX, Symfony's HttpCache, Fastly and Cloudflare, plus an interface for your own
  • a queued CacheInvalidator
  • response tagging with pluggable tag header formatters

FOSHttpCacheBundle wires the library into Symfony: configuration, the invalidator as a service, tag headers on responses, and invalidation rules.

This package plays the same role for Craft and Laravel. Drivers below lists its parts.

The bundle can't be used here because it needs Symfony's FrameworkBundle. This package doesn't yet have the bundle's path-based cache-control rules or user-context hashing.

Drivers

Core fills ResponseCacheInfo with the response's tags and expiry. A driver uses the package for the rest:

  • HttpCache facade (HttpCacheManager): named FOSHttpCache clients from config/http-cache.php. HttpCache::invalidator() purges by tag or URL; HttpCache::extend() adds drivers.
  • CachePolicy (default DefaultCachePolicy): whether a request may be served from a cache and whether a response may be stored. Drivers check isResponseCacheable() before adding their own header, such as CDN-Cache-Control.
  • SendTagHeader: writes ResponseCacheInfo::getTags() to the configured tag header, such as Cache-Tag or xkey.
  • Invalidation listeners (this PR), such as InvalidateElementTags: turn core's change events ([6.x] Add change events for caches and CDNs cms#21048) into HttpCache::invalidator() calls.

Related issues

Listens to ElementCachesInvalidated, skips drafts and revisions, and queues
a tag purge on the default client if it can purge by tag. Every client's
queue is flushed after the request or command, and after each queued job;
failed purges are reported instead of thrown.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1fc17-c592-763a-a600-e76afae5cd1f
timkelty and others added 2 commits November 24, 2026 05:40
In FOSHttpCache, purging means URL purges (PurgeCapable). The listener calls
invalidateTags() on TagCapable clients, and core's event is
ElementCachesInvalidated.
Core renamed ResponseTagger to ResponseCacheInfo and moved it to
CraftCms\Cms\Http.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1fc17-c592-763a-a600-e76afae5cd1f
Co-authored-by: Amp <amp@ampcode.com>

Copy link
Copy Markdown
Contributor Author

Landed directly on main (807f69e), with InvalidateElementTags and core's ResponseCacheInfo. While the package is new, we're committing to main instead of opening PRs.

@timkelty timkelty closed this Nov 24, 2026
@timkelty
timkelty deleted the element-invalidation branch November 24, 2026 05:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant